package th.retrofit.lib;

import java.util.LinkedList;
import java.util.List;

public class Solution94 {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> results = new LinkedList<>();
        inorderTraversal(results, root);
        return results;
    }

    private void inorderTraversal(List<Integer> results, TreeNode root) {
        if (root == null) return;
        if (root.left != null) {
            inorderTraversal(results, root.left);
        }
        results.add(root.val);
        if (root.right != null) {
            inorderTraversal(results, root.right);
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = null;
        TreeNode treeNode3 = new TreeNode(2);
        TreeNode treeNode4 = new TreeNode(3);
        TreeNode root = TreeNode.array2Tree(treeNode1, treeNode2, treeNode3, treeNode4);
        List<Integer> results = new Solution94().inorderTraversal(root);
        for (int i = 0; i < results.size(); i++) {
            System.out.print(results.get(i));
            System.out.print(", ");
        }
        System.out.println();
    }

}
